//
//  MainViewController.swift
//  WeiboDemo
//
//  Created by Quentin Zang on 2020/3/12.
//  Copyright © 2020 臧乾坤. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    // MARK: - 监听方法
    ///  如果a单纯使用private，运行循环无法正常发送消息，导致崩溃
    ///  如果使用@objc修饰符号，可以保证运行循环能够发送此消息，即使函数被标记为private
    @objc private func clickComposedButton() {
        print("点我了")
    }
    
    // MARK: - 视图生命周期函数
    override func viewWillAppear(_ animated: Bool) {
        // 会创建tabBar中所有控制器对应的按钮
        super.viewWillAppear(animated)
        print(tabBar.subviews)
        
        // 将撰写按钮弄到最前面
        tabBar.bringSubviewToFront(composedButton)
    }
    override func viewDidLoad() {
        super.viewDidLoad()

        addChildViewControllers()
        // 添加控制器，并不会创建tabBar中的按钮
        // 控件的创建是懒加载的
        print(tabBar.subviews)
        setupComposedButton()
        // Do any additional setup after loading the view.
    }
    
    
    // MARK: - 懒加载控件
    private lazy var composedButton: UIButton = UIButton(imageName: "tabbar_compose_icon_add", backgroundImageName: "tabbar_compose_button")
//    {
//        // 自定义样式的按钮
//        let button = UIButton()
//        button.setImage(UIImage(named: "tabbar_compose_icon_add"), for: .normal)
//        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), for: .highlighted)
//        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), for: .normal)
//        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), for: .highlighted)
//
//        // 会根据背景图片的大小调整大小
//        button.sizeToFit()
//
//        return button
//    }()

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}


// MARK: - 设置界面
// extension 类似于 oc 的分类，分类中不能定义”存储性“属性！swift也是如此
extension MainViewController {

    ///  设置撰写按钮
    private func setupComposedButton() {
        //  1.添加按钮
        tabBar.addSubview(composedButton)
        
//        // 2.调整按钮
//        let count = childViewControllers.count
//        let w = tabBar.bounds.width / (CGFloat)count
//        composedButton.frame = CGRectInset(tabBar.bounds,2*w,0)
        composedButton.frame = CGRect(x: (UIScreen.main.bounds.size.width - 50) / 2, y: 8, width: 50, height: 50)
        
        // 3.添加监听方法
        composedButton.addTarget(self, action: #selector(clickComposedButton), for: .touchUpInside)
        
    }

    ///  添加所有的控制器
    private func addChildViewControllers() {
        
        addChildViewController(viewController: HomeTableViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(viewController: MessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        
        addChild(UIViewController())
        
        addChildViewController(viewController: DiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(viewController: ProfileTableViewController(), title: "我", imageName: "tabbar_profile")
    }
    
    /// 添加控制器
    /// - Parameters:
    ///   - viewController: vc
    ///   - title: 标题
    ///   - imageName: 图像名称
    private func addChildViewController(viewController: UIViewController, title: String, imageName: String) {

        // 设置标题 - 由内至外设置的
        viewController.title = title
        
        // 设置图像
        viewController.tabBarItem.image = UIImage(named: imageName)
        
        // 导航控制器
        let nav = UINavigationController(rootViewController: viewController)
        //nav.tabBarItem.title = "首页" //设置底部tabbar的标题
        addChild(nav)
    }
    
}
